<!DOCTYPE html>
<html lang="en" th:with="title='评论管理',active='comments'" xmlns:th="http://www.thymeleaf.org">
<header th:replace="admin/header::headerFragment(${title},${active})"></header>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="admin/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">评论管理</h4>
                    </div>
                    <div class="col-md-12">
                        <table class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>评论内容</th>
                                <th>评论人</th>
                                <th>评论时间</th>
                                <th>评论人邮箱</th>
                                <th>评论人网址</th>
                                <th>评论状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <th:block th:each="comment : ${comments.records}">
                                <tr th:attr="id=${comment.id}">
                                    <td>
                                        <a target="_blank"
                                           th:href="@{${commons.siteUrl('/article/')}+${comment.articleId}+'#comments'}"
                                           th:utext="${commons.article(comment.content)}"></a>
                                    </td>
                                    <td th:text="${comment.author}"></td>
                                    <td th:text="${#dates.format(comment.createTime, 'MM月dd日HH:mm')}"></td>
                                    <td th:text="${comment.mail}"></td>
                                    <td><a target="_blank" th:href="@{${comment.url}}" th:text="${comment.url}"></a>
                                    </td>
                                    <td>
                                        <th:block th:if="${comment.status == 1}">
                                            <span class="label label-success">审核通过</span>
                                        </th:block>
                                        <th:block th:if="${comment.status == 0}">
                                            <span class="label label-default">未审核</span>
                                        </th:block>
                                    </td>
                                    <td>
                                        <th:block th:if="${comment.status == 0}">
                                            <a class="btn btn-primary  btn-sm waves-effect waves-light m-b-5"
                                               href="javascript:void(0)"
                                               th:onclick="updateStatus([[${comment.id}]]);"></i>
                                                <i class="fa fa-edit"></i> <span>通过</span></a>
                                        </th:block>
                                        <a class="btn btn-danger btn-sm waves-effect waves-light m-b-5"
                                           href="javascript:void(0)"
                                           th:onclick="delComment([[${comment.id}]]);"><i
                                                class="fa fa-trash-o"></i> <span>删除</span></a>
                                    </td>
                                </tr>
                            </th:block>
                            </tbody>
                        </table>
                        <div th:replace="comm/macros :: pageAdminNav(${comments})"></div>
                    </div>
                </div>
                <div th:replace="admin/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="admin/footer :: footer"></div>
<script type="text/javascript">
    let tale = new $.tale();

    function delComment(id) {
        tale.alertConfirm({
            title: '确定删除该评论吗?',
            then: function () {
                tale.post({
                    url: '/api/v1/admin/comments/delete',
                    data: {id: id},
                    success: function (result) {
                        if (result && result.success) {
                            tale.alertOkAndReload('评论删除成功');
                        } else {
                            tale.alertError(result.msg || '评论删除失败');
                        }
                    }
                });
            }
        });
    }

    function updateStatus(id, status) {
        tale.post({
            url: '/api/v1/admin/comments/status',
            data: {id: id, status: status},
            success: function (result) {
                if (result && result.success) {
                    tale.alertOkAndReload('评论状态设置成功');
                } else {
                    tale.alertError(result.msg || '评论设置失败');
                }
            }
        });
    }
</script>
</body>
</html>